<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="CHT模号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="chtMoldNum">
              <j-search-select-tag placeholder="请输入CHT模号" v-model="model.chtMoldNum" :dictOptions="dictOptions" @change="selectchange()"></j-search-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="产品名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productName">
              <a-input v-model="model.productName" placeholder="请输入产品名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="产品图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productPic">
              <j-image-upload isMultiple  v-model="model.productPic" ></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="产品材料/类型/编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="number">
              <a-input v-model="model.number" placeholder="请输入产品材料/类型/编号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="收缩率" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shrinkageRate">
              <a-input v-model="model.shrinkageRate" placeholder="请输入收缩率"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="产品尺寸" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productSize">
              <a-input v-model="model.productSize" placeholder="请输入产品尺寸"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <h2>产品重量</h2>
          </a-col>
          <a-col :span="18">
            <a-form-model-item label="产品重量报价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productWeightQuotation">
              <a-input v-model="model.productWeightQuotation" placeholder="请输入产品重量报价"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="18">
            <a-form-model-item label="产品重量实际" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productWeightAct">
              <a-input v-model="model.productWeightAct" placeholder="请输入产品重量实际"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <h2>水口重量</h2>
          </a-col>
          <a-col :span="18">
            <a-form-model-item label="水口重量报价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="waterWeightQuotation">
              <a-input v-model="model.waterWeightQuotation" placeholder="请输入水口重量报价"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="18">
            <a-form-model-item label="水口重量实际" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="waterWeightAct">
              <a-input v-model="model.waterWeightAct" placeholder="请输入水口重量实际"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'YlProductInfoForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        productInformation:{},
        dictOptions:[],
        testres:[],
        labelCol: {
          xs: { span: 24 },
          sm: { span: 8 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 13 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/ch/ylProductInfo/add",
          edit: "/ch/ylProductInfo/edit",
          queryById: "/ch/ylProductInfo/queryById",
          queryAllMoldNum: "ch/ylProjectInfo/queryAllMoldNum",
          queryByChtMoldNum: "/ch/ylProjectInfo/queryByChtMoldNum"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
      this.queryAllMoldNum();
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      queryAllMoldNum(){
        getAction(this.url.queryAllMoldNum).then((res)=>{
              if(res.success){
                this.testres = res.result;
                for(let i = 0;i<this.testres.length;i++){
                  this.dictOptions.push({text:this.testres[i],value:this.testres[i]})
                }
                console.log(this.testres);
              }
            })
      },
      selectchange(){
        console.log(this.model.chtMoldNum);
        getAction(this.url.queryByChtMoldNum,{chtMoldNum:this.model.chtMoldNum}).then((res)=>{
          if(res.success){
            console.log(res.result);
            this.productInformation = res.result.productInformation;
            console.log(this.productInformation);
            this.model.productName = this.productInformation.productName;
            this.model.shrinkageRate = this.productInformation.shrinkage;
            this.model.number = this.productInformation.productNumber;
            this.model.productWeightQuotation = this.productInformation.partWeight;
            this.model.waterWeightQuotation = this.productInformation.nozzleWeight;
            //this.model.productSize = this.productInformation.size;
            this.$set(this.model,'productSize',this.productInformation.size);
          }
        })
       },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>